package com.dowsure.apisaas.api;

import com.dowsure.apisaas.enums.BorrowerTypeEnums;
import com.dowsure.apisaas.enums.MarketplaceEnums;
import com.dowsure.apisaas.enums.MethodEnums;
import com.dowsure.apisaas.exception.ApiException;
import com.dowsure.apisaas.model.*;
import com.dowsure.apisaas.util.ApiClient;
import com.dowsure.apisaas.util.ApiResponse;
import com.dowsure.apisaas.util.RequestParamValidatedUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: apisaas-sdk
 * @description:
 * @author: Dowsure
 * @create: 2022-02-15 11:41
 **/
public class SellerApi {

    private ApiClient apiClient;

    public SellerApi(ApiClient apiClient){
        this.apiClient = apiClient;
    }

    /**
     * 1、请求发送offer（API-C01）
     * 用户可以自行获取卖家的SellerID，并且请求Dowsure给卖家发送offer。
     *   a、需要提供借款人（实控人）基本信息。
     *   b、需要提供借款人（实控人）所有店铺信息。
     *   b、需要提供每个店铺预授信额度（范围）信息。
     * 卖家的offer发送结果，用户可以自行调用 查询offer发送状态（API-C02） 查询或者等待Dowsure通知（需要用户提前设置 webhook设置（API-C14））。
     * 注：Dowsure 获取到借款人（实控人）的基本信息后需要通过 设置校验借款人信息（API-C04） 对数据进行幂等校验，保证数据一致。避免账户锁定、借款人姓名、证件等信息出错，导致账户、授信错误造成经济损失
     * @author Dowsure
     * @param sellerOfferRequestParams {@link SellerOfferRequestParams}
     * @return {@link ApiResponse < SellerOfferResponseParams >}
     * @throws ApiException
     */
    public ApiResponse<SellerOfferResponseParams> offer(SellerOfferRequestParams sellerOfferRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(sellerOfferRequestParams);
        if (BorrowerTypeEnums.ENTERPRISE.equals(sellerOfferRequestParams.getBorrowerType())){
            RequestParamValidatedUtils.notBlank(sellerOfferRequestParams.getLegalPersonName(), "legalPersonName（企业法人姓名）");
            RequestParamValidatedUtils.notBlank(sellerOfferRequestParams.getBizLicenseNumber(), "bizLicenseNumber（借款人企业营业执照号）");
        }
        RequestParamValidatedUtils.validated(sellerOfferRequestParams.getSellerList());
        for (SellerOfferSeller seller : sellerOfferRequestParams.getSellerList()){
            if (MarketplaceEnums.US.equals(seller.getRegion())){
                RequestParamValidatedUtils.notBlank(seller.getRoutingNumber(),"routingNumber（美国回款路线）");
            } else if (MarketplaceEnums.DE.equals(seller.getRegion())){
                RequestParamValidatedUtils.notBlank(seller.getSwiftCode(), "swiftCode（收款银行代码）");
            }
        }
        String localVarPath = "/v1/seller/offer";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, sellerOfferRequestParams, headerParams);
        return apiClient.execute(call);
    }

    /**
     * 2、查询offer发送状态（API-C02）
     * 用户请求offer发送后，可以通过该接口查询offer的发送状态。
     *   a、需要提供卖家编号。
     * @author Dowsure
     * @param sellerOfferStatusRequestParams {@link SellerOfferStatusRequestParams}
     * @return {@link ApiResponse < SellerOfferStatusResponseParams >}
     * @throws ApiException
     */
    public ApiResponse<List<SellerOfferStatusResponseParams>> offerStatus(List<SellerOfferStatusRequestParams> sellerOfferStatusRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(sellerOfferStatusRequestParams);
        String localVarPath = "/v1/seller/offer-status";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.GET, sellerOfferStatusRequestParams, headerParams);
        return apiClient.execute(call);
    }


    /**
     * 7、获取卖家店铺数据（API-C06）
     * 当卖家在Amazon提交贷款申请时，会跳转到Dowsure进行后续操作，此时Dowsure会获取到卖家的店铺数
     * 据，用户可以通过该接口获取卖家店铺标签化数据。
     * 需要提供Dowsure 卖家申请单编号。
     * @author Dowsure
     * @param sellerShopDataRequestParams {@link SellerShopDataRequestParams}
     * @return {@link ApiResponse < SellerShopDataResponseParams >}
     * @throws ApiException
     */
    public ApiResponse<SellerShopDataResponseParams> shopData(SellerShopDataRequestParams sellerShopDataRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(sellerShopDataRequestParams);
        String localVarPath = "/v1/seller/shop-data";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.GET, sellerShopDataRequestParams, headerParams);
        return apiClient.execute(call);
    }


    /**
     * 14、校验卖家支付公司信息（API-C13）
     * 用户可以获取卖家支付公司信息核对身份。
     * @author Dowsure
     * @param sellerPspAccountRequestParams {@link SellerPspAccountRequestParams}
     * @return {@link ApiResponse < SellerPspAccountResponseParams >}
     * @throws ApiException
     */
    public ApiResponse<SellerPspAccountResponseParams> pspAccount(SellerPspAccountRequestParams sellerPspAccountRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(sellerPspAccountRequestParams);
        if (BorrowerTypeEnums.ENTERPRISE.equals(sellerPspAccountRequestParams.getBorrowerType())){
            RequestParamValidatedUtils.notBlank(sellerPspAccountRequestParams.getLegalPersonName(), "legalPersonName（企业法人姓名）");
            RequestParamValidatedUtils.notBlank(sellerPspAccountRequestParams.getBizLicenseNumber(), "bizLicenseNumber（借款人企业营业执照号）");
        }
        String localVarPath = "/v1/seller/psp-account";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, sellerPspAccountRequestParams, headerParams);
        return apiClient.execute(call);
    }


    /**
     * 15、查询卖家平台账户状态（API-D01）
     * 用户可以实时查询卖家的账户锁定状态。
     * 若卖家多店铺申请贷款，其中只要有一个店铺账户状态为【未锁定】，则返回lockStatus=UNLOCKED。
     * @author Dowsure
     * @param sellerAccountLockRequestParams {@link SellerAccountLockRequestParams}
     * @return {@link ApiResponse < SellerAccountLockResponseParams >}
     * @throws ApiException
     */
    public ApiResponse<SellerAccountLockResponseParams> accountLock(SellerAccountLockRequestParams sellerAccountLockRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(sellerAccountLockRequestParams);
        String localVarPath = "/v1/seller/account-lock";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, sellerAccountLockRequestParams, headerParams);
        return apiClient.execute(call);
    }

    /**
     * 23、获取卖家店铺评分信息（API-C23）
     * 用户可以调用该接口获取卖家的评分信息，评分维度为分：
     * 合规、合规排名、
     * 评价、评价排名、
     * 库存、库存排名、
     * 增长、增长排名、
     * 回款、回款排名。
     * （仅限同行业）
     * @author Dowsure
     * @param sellerShopScoreRequestParams {@link SellerShopScoreRequestParams}
     * @return {@link ApiResponse < SellerAccountLockResponseParams >}
     * @throws ApiException
     */
    public ApiResponse<SellerShopScoreResponseParams> shopScore(SellerShopScoreRequestParams sellerShopScoreRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(sellerShopScoreRequestParams);
        String localVarPath = "/v1/seller/shop-score";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.GET, sellerShopScoreRequestParams, headerParams);
        return apiClient.execute(call);
    }
}
